package entityclasses;

import builders.ProfileBuilder;
import entityclasses.Profile;
import socialappexception.FriendRequestException;
import java.util.*;
import javax.swing.JOptionPane;
import factories.ObjectFactory;
import observers.RequestObserver;


public class Person extends Observable implements RequestObserver{

	private int id;
	private String firstName;
	private String lastName;
	private ProfileBuilder builder;
        private Profile profile;

    public Person ()
    {   
        setProfile("", (RelationshipStatus)ObjectFactory.create(RelationshipStatus.class));
    }

    public Person(int id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        setProfile("", null);
    }
        
    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * @return the profile
     */
    public Profile getProfile() {
        return profile;
    }

    /**
     * @param profile the profile to set
     */
    public void setProfile(String profileName, RelationshipStatus relationshipStatus) {
        this.profile = createProfile(profileName,relationshipStatus);
    }
    private Profile createProfile(String profileName, RelationshipStatus relationshipStatus)
    {
        this.builder = new ProfileBuilder();
        this.builder.setCreateDate();
        this.builder.setFriendRequests();
        this.builder.setFriends();
        this.builder.setGroups();
        this.builder.setProfileName(profileName);
        this.builder.setRelateds();
        this.builder.setRelationship(relationshipStatus);
        this.builder.setShares();
        
        return this.builder.getProfile();
        
    }
    @Override
    public String toString()
    {
        return +getId()+" "+getFirstName()+" "+getLastName();
    }

    public void register(SocialApp program) {
        program.getRegisteredPersons().add(this);
    }

    @Override
    public void addAsFriend(Person addTo) {
        if(!addTo.getProfile().getFriendRequests().contains(this))
        addTo.getProfile().getFriendRequests().add(this);
        else 
            try {
            throw new FriendRequestException(addTo.getFirstName()+" " + addTo.getLastName() + " kisisi ile zaten arkadassiniz.");
        } catch (FriendRequestException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(),"Hata", JOptionPane.ERROR_MESSAGE);
            //Logger.getLogger(Person.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void acceptRequest(Person requestFrom) {
        
        if (this.getProfile().getFriendRequests().contains(requestFrom))
        {
            this.getProfile().getFriendRequests().remove(requestFrom);
            this.getProfile().getFriends().add(requestFrom);
            //requestFrom.getProfile().getFriends().add(this);
            requestFrom.update(this, null);
        }else
            try {
            throw new FriendRequestException(requestFrom.getFirstName()+" " + requestFrom.getLastName() + "bekleme listesinde yok.");
        } catch (FriendRequestException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(),"Hata", JOptionPane.ERROR_MESSAGE);
            //Logger.getLogger(Person.class.getName()).log(Level.SEVERE, null, ex);
        }    
    }

    public void cancelRequest(Person requestFrom) {

       
        try{
        
        if (this.getProfile().getFriendRequests().contains(requestFrom)) {
            this.getProfile().getFriendRequests().remove(requestFrom);
        }else
            throw new FriendRequestException(requestFrom.getFirstName() + " " + requestFrom.getLastName() + "bekleme listesinde yok.");
       
            
        } catch (FriendRequestException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Hata", JOptionPane.ERROR_MESSAGE);
        }
    }
    @Override
    public void update(Observable o, Object arg) {
        this.getProfile().getFriends().add((Person)o);
    }



}
